package com.jiukuaidao.zhulin.module.user.presenter;

import android.os.Bundle;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.zhulin.AppContext;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.api.ApiGetFindPwd;
import com.jiukuaidao.zhulin.data.TasksRepository;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.user.contract.FindPasswordContract;
import com.ta.utdid2.android.utils.StringUtils;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/27
 * Description :
 */
public class FindPasswordPresenter extends BasePresenter<FindPasswordContract.View, TasksRepository> implements FindPasswordContract.Presenter {
    public FindPasswordPresenter(TasksRepository repository, FindPasswordContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {
        loadData();
    }

    @Override
    public void loadData() {
        if (StringUtils.isEmpty(mView.getMobile())) {
            mView.toast(AppContext.getContext().getString(R.string.phone_empty));
            return;
        }
        if (mView.getMobile().length() != 11) {
            mView.toast(AppContext.getContext().getString(R.string.phone_error_hint));
            return;
        }
        if (StringUtils.isEmpty(mView.getVeriCode())) {
            mView.toast(AppContext.getContext().getString(R.string.code_empty_hint));
            return;
        }
        mRepository.loadData(new ApiGetFindPwd(mView.getMobile(), mView.getVeriCode(), new MyRetroCallBack() {
            @Override
            public void onSuccess(Object result, String errorCode, String msg) {
                mView.toast(msg);
                Bundle bundle = new Bundle();
                bundle.putString("phone", mView.getMobile());
                bundle.putString("pwd_code", mView.getVeriCode());
                mView.setJump(bundle);
            }

            @Override
            public void onFail(Object result, String errorCode, String msg) {
                mView.toast(msg);
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                mView.toast(resCode + "");
            }
        }));
    }
}
